//
//  CZStatusView.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/20.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
import YYText
class CZStatusView: UIView {
    
    //当前控件的底部的约束
    var bottomCon: Constraint?

    //属性,设置控件的内容
    var statusViewModel: CZStatusViewModel? {
        didSet{
            
            //设置头像
            headImageView.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            //设置名字
            nameLabel.text = statusViewModel?.status?.user?.name
            //设置会员的图标
            vipImageView.image = statusViewModel?.memberImage
            //设置时间与来源(后面需要重新设置)
            timeLabel.text = statusViewModel?.creatAtText
            //设置来源
            sourceLabel.text = statusViewModel?.sourceText
            
            //设置微博的正文的内容
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            //设置pictureView
            //设个约束的设置一定要放在最外层
             bottomCon?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0{
//                
//                //判断是否为第一张,设置itemSize 为视图大小
//                
//                if pic_urls.count == 1{
//                    
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statusViewModel!.originalViewSize
//                    
//                    
//                }else{
//                    
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    
//                    layout.itemSize = CGSize(width: CZPictureViewItenWH, height: CZPictureViewItenWH)
//                    
//                    
//                }
//                
//                //上面的设置
//                
//                
//                
//                //说明有数据
//                pictureView.pic_urls = pic_urls
                //设置约束
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo(statusViewModel?.originalViewSize ?? CGSizeZero)
//                })
                
                
                //1.设置配图的数据和itemSize
                
                pictureView.dataInfo = (pic_urls,statusViewModel!.originalViewSize)
                
                
                //2.显示控件
                pictureView.hidden = false
                
                //跟新底部的约束
               
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                   bottomCon = make.bottom.equalTo(pictureView.snp_bottom).offset(CZHomeCellMargin).constraint
                })
                
                
                
                
            }else{
                
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCon = make.bottom.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin).constraint
                    
                })
                
                
            }
            
            
        }
        
    }
   
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
     setupUI()
        
        
//        self.backgroundColor = RandomColor
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //设置一个randomColor
//        backgroundColor = RandomColor
        
        //设置界面,进行自定义
        
        //创建的空间1.头像 2.名字 3.vip 4.时间 5.来源
        
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        
        addSubview(headImageView)
        
        addSubview(nameLabel)
        
        addSubview(vipImageView)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        addSubview(verifiedImageView)
        //提那家imageView
        addSubview(pictureView)
        
        
        //设置约束
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(CZHomeCellMargin)
            
//            make.leading.equalTo(<#T##other: CGFloat##CGFloat#>)
            
            make.size.equalTo(CGSize(width: CZHomeCellHeadImageWH, height: CZHomeCellHeadImageWH))
            
            
            
            
            
            
        }
        
        //约束名字
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(headImageView.snp_trailing).offset(CZHomeCellMargin)
            
            make.top.equalTo(headImageView)
            
            
            
        }
        //约束vip
        vipImageView.snp_makeConstraints { (make) -> Void in
            
            //设置vip的约束
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(CZHomeCellMargin)
            
            make.centerY.equalTo(nameLabel)
            
            
        }
        
        //设置时间label
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel)
            
            make.bottom.equalTo(headImageView)
            
            
        }
        
        //设置认证imageView的越是
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing).offset(-2)
            
            make.centerY.equalTo(headImageView.snp_bottom).offset(-2)
            
        }
        
        
        //设置source的约束
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(CZHomeCellMargin)
            make.centerY.equalTo(timeLabel)
            
            
//            make.top.equalTo(timeLabel)
            
        }
        //设置正文的约束
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImageView.snp_bottom).offset(CZHomeCellMargin)
            make.leading.equalTo(headImageView)
            
            
        }
        
        //设置picture的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin)
            make.leading.equalTo(contentLabel)
            //以下代码仅供测试
            make.size.equalTo(CGSizeMake(100, 100))
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            //这里设置了一个margin,所以有间隔
           bottomCon =  make.bottom.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin).constraint
        }
        
        
        
        
    }
    
    
    
    //头像
        private lazy var headImageView: UIImageView = {
    
           let imageView = UIImageView()
            
            
            imageView.image = UIImage(named: "avatar_default_small")
    
    
    
            return imageView
    
    
            
        }()
    //
    
    //名字
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: CZHomeCellNameFontSize)
        
        
        label.text = "哈哈"
        
        return label
        
        
        
        
    }()
    
    
    //创建vip图像
    
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //创建时间label
    
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: CZHomeCellTimeFontSize)
        
        label.text = "刚刚"
        
        
        return label
        
    }()
    
    //创建来源的label
    
    
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: CZHomeCellTimeFontSize)
        
        
        label.text = "来自刘吉庆你买不起的mac"
        
        return label
        
        
    }()
    
    private lazy var contentLabel: YYLabel = {
       
//        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: CZHomeCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * CZHomeCellMargin)
        let label = YYLabel()
        label.userInteractionEnabled = true

        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZHomeCellContentFontSize)
        label.preferredMaxLayoutWidth = SCREENW - 2 * CZHomeCellMargin
//        label.backgroundColor = RandomColor
        label.numberOfLines = 0
        return label
        
        
        
    }()
    
    //添加认证的iamgeView
    
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //懒加载view
    private lazy var pictureView: CZPictureView = CZPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
    
    
    

}
